package com.lyc.api.controller;

import com.lyc.api.VO.RankVO;
import com.lyc.api.VO.ResultVO;
import com.lyc.api.service.BookService;
import com.lyc.api.utils.ResultVOUtil;
import com.lyc.common.enums.ChannelPassEnum;
import com.lyc.common.enums.SearchType;
import com.lyc.common.model.BookInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 廖师兄
 * 2017-05-26 20:46
 */
@RestController
@Slf4j
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @GetMapping("/list")
    public ResultVO list(@RequestParam("categoryId") Integer categoryId,
                         @RequestParam("page") Integer page,
                         @RequestParam("size") Integer size) {
        List<BookInfo> bookInfoList = bookService.list(categoryId, page, size);

        HashMap<String, List<BookInfo>> mapResponse = new HashMap<>();
        mapResponse.put("books", bookInfoList);
        return ResultVOUtil.success(mapResponse);
    }

    @GetMapping("/detail")
    public ResultVO detail(@RequestParam("bookId") String bookId) {
        BookInfo bookInfo = bookService.detail(bookId);
        return ResultVOUtil.success(bookInfo);
    }

    @GetMapping("/search")
    public ResultVO search(@RequestParam String keyWord,
                           @RequestParam Integer type) {
        List<BookInfo> bookInfoList = new ArrayList<>();

        if (type.equals(SearchType.name.getCode())) {
            bookInfoList = bookService.searchByName(keyWord);
        }else if (type.equals(SearchType.author.getCode())){
            bookInfoList = bookService.searchByAuthor(keyWord);
        }
        HashMap<String, List<BookInfo>> mapResponse = new HashMap<>();
        mapResponse.put("books", bookInfoList);
        return ResultVOUtil.success(mapResponse);
    }

    @GetMapping("/rank")
    public ResultVO rank(@RequestParam String channel,
                         @RequestParam Integer size) {
        Map<String, List<RankVO>> response = new HashMap<>();

        //未审核通过的渠道, 返回空内容
        if (!ChannelPassEnum.contain(channel)) {
            response.put("categorys", new ArrayList<>());
            return ResultVOUtil.success(response);
        }
        List<RankVO> rankVOList = bookService.rank(size);

        response.put("categorys", rankVOList);
        return ResultVOUtil.success(response);
    }

}
